package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.NewsComment;
import com.qf.entity.NewsCommentExample;
import com.qf.mapper.NewsCommentMapper;
import com.qf.service.NewsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/10/6 11:34
 */
@Service
public class NewsCommentServiceImpl implements NewsCommentService {

    @Autowired
    private NewsCommentMapper newsCommentMapper;
    @Override
    public PageInfo<NewsComment> findByNewsId(Integer page,Integer rows,Integer newsId) {
        NewsCommentExample example=new NewsCommentExample();
        NewsCommentExample.Criteria criteria = example.createCriteria();
        criteria.andNewsIdEqualTo(newsId);

        PageHelper.startPage(page,rows);
        List<NewsComment> list = newsCommentMapper.selectByExample(example);
        PageInfo<NewsComment>pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void add(NewsComment newsComment) {
        int i = newsCommentMapper.insert(newsComment);
        if (i!=1){
            System.out.println("评论失败");
            throw new RuntimeException("评论失败");
        }
    }

    @Override
    public void removeByNewsId(Integer newsId) {
        NewsCommentExample example=new NewsCommentExample();
        NewsCommentExample.Criteria criteria = example.createCriteria();
        criteria.andNewsIdEqualTo(newsId);
        int i = newsCommentMapper.deleteByExample(example);
    }
}
